package com.estronger.shopmanage.adapter.batchpg;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.base.MyApplication;
import com.estronger.shopmanage.bean.batch.BatchPgGoodsItem;
import com.estronger.shopmanage.manager.NetManager;
import com.estronger.shopmanage.utils.CustomLinearLayoutManager;
import com.soonfor.evaluate.base.EvalBaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Soonfor on 2017/6/6.
 * 批量派工确认列表
 */

public class BatchPgGoodsAdapter extends EvalBaseAdapter<BatchPgGoodsAdapter.GoodHolder, BatchPgGoodsItem> implements View.OnClickListener {

    Activity mActivity;
    int parentIndex;//任务在列表的位置
    ArrayList<BatchPgGoodsItem> goodsList;
    GoodsAdapterListener adapterListener;
    int dataType = 0;

    public BatchPgGoodsAdapter(Activity context) {
        super(context);
        this.mActivity = context;
    }

    public void setAdapterListener(GoodsAdapterListener adapterListener, int dataType) {
        this.adapterListener = adapterListener;
        this.dataType = dataType;
    }

    @Override
    public void notifyDataSetChanged(List<BatchPgGoodsItem> dataList) {}
    public void notifyDataSetChanged(ArrayList<BatchPgGoodsItem> dataList, int parentIndex) {
        this.goodsList = dataList;
        this.parentIndex = parentIndex;
        notifyDataSetChanged();
    }

    @Override
    public BatchPgGoodsAdapter.GoodHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.adapter_pg_batch_good, parent, false);
        GoodHolder holder = new GoodHolder(view);
        return holder;
    }



    @Override
    public void onBindViewHolder(@NonNull GoodHolder holder, int position) {
        holder.conditonAdapter.setCheckedListener(new BatchPgGoodsConditionAdapter.ConditonCheckedListener() {
            @Override
            public void onCheckListener(ArrayList<BatchPgGoodsItem.GoodsCondition> conditionList, int goodsIndex) {
                try {
                    goodsList.get(goodsIndex).setConditionitem(conditionList);
                    adapterListener.onContionChange(goodsList, parentIndex);
                }catch (Exception e){
                    e.fillInStackTrace();
                }

            }
        });
        holder.setData(goodsList.get(position), position);
    }

    @Override
    public int getItemCount() {
        return goodsList == null ? 0 : goodsList.size();
    }

    @Override
    public void onClick(View v) {
        GoodHolder vh = (GoodHolder) v.getTag();
        switch (v.getId()){
            case R.id.rl_check:

                break;
        }

    }

    public class GoodHolder extends RecyclerView.ViewHolder {

        public int positon;
        TextView tvfGoodsName, tvfGoodsSize, tvfGoodsCount, tvfUnitname, tvfContionTitle;
        ImageView ivfGoodsPic;
        RecyclerView rvfConditons;
        LinearLayout llfHiddenCondition;
        RelativeLayout rlfUpOrDown, rlfTou;
        ImageView ivfShowConditions;
        boolean isShowing;
        private CustomLinearLayoutManager conditionManager;
        private BatchPgGoodsConditionAdapter conditonAdapter;

        public GoodHolder(View view) {
            super(view);
            rlfTou = (RelativeLayout) view.findViewById(R.id.rlfTou);
            llfHiddenCondition = (LinearLayout) view.findViewById(R.id.llfHiddenCondition);
            tvfContionTitle = view.findViewById(R.id.tv_contion_title);
            rlfUpOrDown = (RelativeLayout) view.findViewById(R.id.rlfUp_Down);
            ivfShowConditions = (ImageView) view.findViewById(R.id.ivfPull);
            isShowing = false;
            tvfGoodsName = (TextView) view.findViewById(R.id.goods_name);
            tvfGoodsSize = (TextView) view.findViewById(R.id.goods_property);
            tvfGoodsCount = (TextView) view.findViewById(R.id.goods_count);
            ivfGoodsPic = (ImageView) view.findViewById(R.id.goods_item_img);
            tvfUnitname = (TextView) view.findViewById(R.id.goods_feature);
            rvfConditons = (RecyclerView) view.findViewById(R.id.rv_conditions);

            conditionManager = new CustomLinearLayoutManager(mActivity);
            conditionManager.setScrollEnabled(false);
            rvfConditons.setLayoutManager(conditionManager);
            conditonAdapter = new BatchPgGoodsConditionAdapter(mActivity);
            rvfConditons.setAdapter(conditonAdapter);
        }
        
        public void setData(final BatchPgGoodsItem goodsItem, int position) {
            this.positon = position;
            rlfUpOrDown.setTag(position);
            tvfGoodsName.setText(goodsItem.getGoodsname());
            tvfGoodsSize.setText(goodsItem.getSizedesc());
            tvfUnitname.setText(goodsItem.getUnitname());

            tvfGoodsCount.setText(goodsItem.getQty());

            if (!goodsItem.getSimplepicfile().equals("")) {
                MyApplication.imageLoader.displayImage(NetManager.getDownloadURL() + "?filepath=" + goodsItem.simplepicfile, ivfGoodsPic, MyApplication.options);
            }
            ArrayList<BatchPgGoodsItem.GoodsCondition> condList = goodsItem.getConditionitem();
            conditonAdapter.notifyDataSetChanged(condList, position);
            if(condList.size() > 0) {//有条件
                llfHiddenCondition.setVisibility(View.VISIBLE);
                tvfContionTitle.setText(goodsItem.getContionTitle(dataType));
                rlfUpOrDown.setVisibility(View.VISIBLE);
                final boolean isShowChild = goodsItem.isShowCondition;
                if (isShowChild) {
                    llfHiddenCondition.setVisibility(View.VISIBLE);
                    ivfShowConditions.setImageResource(R.drawable.pull_up_icon);
                } else {
                    llfHiddenCondition.setVisibility(View.GONE);
                    ivfShowConditions.setImageResource(R.drawable.pull_down_icon);
                }
                rlfUpOrDown.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (isShowing) {
                            goodsItem.isShowCondition = false;
                            llfHiddenCondition.setVisibility(View.GONE);
                            ivfShowConditions.setImageResource(R.drawable.pull_down_icon);
                            isShowing = false;
                        } else {
                            llfHiddenCondition.setVisibility(View.VISIBLE);
                            ivfShowConditions.setImageResource(R.drawable.pull_up_icon);
                            goodsItem.isShowCondition = true;
                            isShowing = true;
                        }
                        notifyDataSetChanged();
                    }
                });
            }else {
                rlfUpOrDown.setVisibility(View.GONE);
                llfHiddenCondition.setVisibility(View.GONE);
            }
        }
    }

    public interface GoodsAdapterListener {
        void onContionChange(ArrayList<BatchPgGoodsItem> goodsItemList, int parentId);//改变条件后更新数据变化
    }
}